package com.dd.service.impl;

import com.dd.mapper.ProductMapper;
import com.dd.mapper.ProductStatMapper;
import com.dd.model.Product;
import com.dd.model.ProductStat;
import com.dd.service.ProductStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品统计服务实现类
 */
@Service
public class ProductStatServiceImpl implements ProductStatService {
    
    @Autowired
    private ProductStatMapper productStatMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    @Override
    public List<ProductStat> findAllProductStats() {
        return productStatMapper.findAll();
    }
    
    @Override
    public ProductStat getProductStatById(Integer statId) {
        return productStatMapper.findById(statId);
    }
    
    @Override
    public List<ProductStat> findProductStatsByProductId(Integer productId) {
        return productStatMapper.findByProductId(productId);
    }
    
    @Override
    public List<ProductStat> findProductStatsByDate(Date date) {
        return productStatMapper.findByDate(date);
    }
    
    @Override
    public List<ProductStat> findProductStatsByDateRange(Date startDate, Date endDate) {
        return productStatMapper.findByDateRange(startDate, endDate);
    }
    
    @Override
    @Transactional
    public boolean saveProductStat(ProductStat productStat) {
        // 查询是否已存在相同商品和日期的统计
        ProductStat existingStat = productStatMapper.findByProductIdAndDate(
                productStat.getProductId(), productStat.getDate());
        
        if (existingStat != null) {
            // 更新现有统计
            productStat.setStatId(existingStat.getStatId());
            return productStatMapper.update(productStat) > 0;
        } else {
            // 添加新统计
            return productStatMapper.insert(productStat) > 0;
        }
    }
    
    @Override
    @Transactional
    public boolean deleteProductStat(Integer statId) {
        return productStatMapper.deleteById(statId) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateProductSaleStats(Integer productId, Integer quantity) {
        // 查找商品
        Product product = productMapper.findById(productId);
        if (product == null) {
            return false;
        }
        
        // 获取当前日期
        Date today = new Date();
        // 处理日期，去掉时分秒
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            today = formatter.parse(formatter.format(today));
        } catch (Exception e) {
            // 处理异常
        }
        
        // 查询今日该商品的统计数据
        ProductStat stat = productStatMapper.findByProductIdAndDate(productId, today);
        
        if (stat == null) {
            // 创建新的统计记录
            stat = new ProductStat();
            stat.setProductId(productId);
            stat.setDate(today);
            stat.setSalesCount(quantity);
            // 计算收入 = 数量 * 单价
            stat.setRevenue(product.getPrice().multiply(new BigDecimal(quantity)));
            
            return productStatMapper.insert(stat) > 0;
        } else {
            // 更新现有统计
            stat.setSalesCount(stat.getSalesCount() + quantity);
            // 计算新增收入
            BigDecimal additionalRevenue = product.getPrice().multiply(new BigDecimal(quantity));
            stat.setRevenue(stat.getRevenue().add(additionalRevenue));
            
            return productStatMapper.update(stat) > 0;
        }
    }
} 